﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Windows;

using Core.Model;
using Core.Model.Column;
using Core.Model.Hierarchy;

namespace UI.Windows.Wizards
{
    public partial class NewResearchWizardWindow : IDisposable
    {
        public NewResearchWizardWindow(IEnumerable<IExaminationModel> models)
        {
			DataContext = _research;

            InitializeComponent();

	        ColumnsListBoxSwitch.SourceItems = new ObservableCollection<IExaminationModel>(models);
        }

		~NewResearchWizardWindow()
	    {
		    Dispose(false);
	    }

		#region Public fields

	    public Research NewResearch
		{
		    get {return _research.Clone();}
	    }

        #endregion

		#region Private fields

		private readonly Research _research = Research.CreateResearch();

		#endregion

        #region Event handlers

        private void AddCustomInformationButtonClick(object sender, RoutedEventArgs e)
        {
            if (TypeWatermarkTextBox.Text != String.Empty)
            {
                NewResearch.CustomInformation.Add(new Tuple<string, string>(TypeWatermarkTextBox.Text, ContentWatermarkTextBox.Text));
            }
        }

		private void WizardFinish(object sender, RoutedEventArgs e)
		{
			dynamic row = new ExpandoObject();
			var dictionary = (IDictionary<String, Object>) row;

			foreach (ExaminationColumn column in NewResearch.Columns)
			{
				dictionary[column.Name] = "";
			}
			NewResearch.Cases.Add(row);
		}

		#endregion

		#region IDisposable implementation

		public void Dispose() 
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing) 
		{
			if (disposing) {
				ColumnsListBoxSwitch.Dispose();
			}
		}

		#endregion
    }
}